package com.andy.nature.ui.activity

import androidx.core.view.isVisible
import com.airbnb.lottie.LottieDrawable
import com.andy.common.base.BaseVMActivity
import com.andy.common.extend.setOnIMClickListener
import com.andy.nature.databinding.ActivityAnimationBinding
import com.andy.nature.ui.adapter.MessageAdapter
import com.andy.nature.utils.AnimUtils
import com.andy.nature.vm.CommonViewModel
import com.andy.toast.extend.showNormalToast

/**
 * Author:Andy
 * Date:2023/4/27 17:51
 * Description:AnimationActivity
 */
class AnimationActivity : BaseVMActivity<ActivityAnimationBinding, CommonViewModel>() {
    private val messageAdapter: MessageAdapter by lazy {
        MessageAdapter()
    }

    override fun viewModelClass(): Class<CommonViewModel> {
        return CommonViewModel::class.java
    }

    override fun getViewBinding(): ActivityAnimationBinding {
        return ActivityAnimationBinding.inflate(layoutInflater)
    }

    override fun initObserve() {
        mViewModel.testLiveData.observe(this) {
            messageAdapter.setData(it)
        }
    }

    override fun initData() {
        mBinding?.apply {
            AnimUtils.startRotate(ivTask)
            lottieAnim.setMinAndMaxFrame(0, 165)
            lottieAnim.repeatCount = LottieDrawable.INFINITE
            lottieAnim.addAnimatorUpdateListener {
                if (lottieAnim.frame == 165) {
                    lottieAnim.frame = 0
                }
                if (lottieAnim.frame == 214) {
                    lottieAnim.setMinAndMaxFrame(215, 327)
                    lottieAnim.repeatCount = LottieDrawable.INFINITE
                    lottieAnim.playAnimation()
                }
                if (lottieAnim.frame == 215 && !btnStartLottie.isVisible) {
                    btnStartLottie.isVisible = true
                }
            }
            lottieAnim.playAnimation()
        }
    }

    override fun initAction() {
        mBinding?.apply {
            vTitle.setClickLeftCallBack {
                finish()
            }
            btnStartMove.setOnIMClickListener {
                AnimUtils.startBgAnim(vTaskBg, targetTask)
            }
            btnStartAlpha.setOnIMClickListener {
                AnimUtils.startAnim(ivTask)
            }
            lottieAnim.setOnIMClickListener {
                lottieAnim.setMinAndMaxFrame(166, 214)
                lottieAnim.repeatCount = 0
                lottieAnim.playAnimation()
            }
            btnStartLottie.setOnIMClickListener {
                "跳转".showNormalToast()
            }
        }
    }

}